package com.geralt.im.commands.impl;

import java.util.Date;

import com.geralt.im.command.Command;
import com.geralt.im.commandenum.Code;
import com.geralt.im.model.Message;
import com.geralt.im.protocol.Response;
import com.geralt.im.server.IMServer;

public class SendCommand extends Command{

	
	@Override
	public Response excute() {
		Message msg = (Message)parameter.get("SEND");
		// validate(msg)
		String receiver = msg.getTo();
		boolean validation = IMServer.map.containsKey(receiver) || "ALL".compareTo(receiver)==0;
		receivers.clear();
		if(validation){
			if("ALL".compareTo(receiver)==0){
				receivers.addAll(IMServer.map.keySet());
			}
			else {
				receivers.add(receiver);
				receivers.add(msg.getFrom());
			}
			Response response = new Response();
			response.setType("SEND");
			msg.setSendTime(new Date());
			response.getParameter().put("MESSAGE", msg);
			response.setStatusCode(Code.OK);
			return response;
		}else {	//Code.BAD_REQUEST
			Response response = new Response();
			response.setStatusCode(Code.BAD_REQUEST);
			response.setType("SEND");
			Message message= new Message();
			message.setContent("Your target is not exist");
			response.getParameter().put("MESSAGE",message);
			return response;
		}
	}

}
